﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    /// <summary>
    /// ChmFileIconPopup.xaml 的交互逻辑
    /// </summary>
    public partial class ChmFileIconPopup : UserControl
    {
        public ChmFileIconPopup()
        {
            InitializeComponent();
        }

        public void SelectImage(int i)
        {
            foreach (var c in baseGrid.Children)
            {
                var button = c as Button;
                if (button == null) continue;

                if (int.TryParse(button.Tag as string, out int tagIndex))
                {
                    if (tagIndex == i)
                    {
                        button.BorderBrush = Brushes.SaddleBrown;
                    }
                    else
                    {
                        button.BorderBrush = Brushes.Transparent;
                    }
                }
                else
                {
                    button.BorderBrush = Brushes.Transparent;
                }
            }
        }
        private void BtnChmFileIcon_Click(object sender, RoutedEventArgs e)
        {
            var selItem = Globals.MainWindow.tvWorkDirectory.SelectedItem;
            if (selItem == null) return;

            var wtvi = selItem as WorkspaceTreeViewItem;
            if (wtvi == null) return;

            if (int.TryParse((sender as Button).Tag.ToString(), out int index))
            {
                wtvi.ChmImageIndex = index;
                wtvi.RefreshIcon();
            }

            var p = this.Parent as Popup;
            if (p != null) p.IsOpen = false;

            Globals.MainWindow.WorkspaceManager.SaveWorkspaceTreeviewToXml();
        }

        private void UserControl_MouseLeave(object sender, MouseEventArgs e)
        {
            var p = this.Parent as Popup;
            if (p == null) return;

            p.IsOpen = false;
        }
    }
}
